const { createLogger, format, transports } = require('winston');
const { combine, timestamp, printf } = format;
const moment = require('moment-timezone');

// 自定义日志格式，包含文件名和行号，并调整时区
const logFormat = printf(({ level, message, timestamp, stack }) => {
  // 调整时区为你所需的时区
  const timezone = 'Asia/Shanghai';
  const localTime = moment(timestamp).tz(timezone).format('YYYY-MM-DD HH:mm:ss.SSS Z');

  return `${localTime} [${level}] - ${message}\n${stack || ''}`;
});

// 配置 winston
const logger = createLogger({
  level: 'error',
  format: combine(
    timestamp(),
    format.errors({ stack: true }), // 使得错误包含堆栈信息
    logFormat
  ),
  transports: [
    new transports.Console(), // 输出到控制台
    new transports.File({ filename: 'error.log' }) // 输出到文件
  ]
});

module.exports = logger;
